'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AdministratorAdminBzCtrl
 * @description
 * # AdministratorAdminBzCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AdministratorAdminBzCtrl', function($scope, $rootScope, openApi, $http, Util, $location, $routeParams, FormSubmit) {
        var selectData, params = {};
        // 如果有回显的数据就用回显的数据
        // 获取选择好的数据
        $scope.param = params;
        // --------------------------------------
        // 选择报销单明细
        $scope.showCertificate = function() {
            var param = {};
            param.type = "证件类别";
            Util.getKeyValue(param, function(data) {
                $scope.resiTypeList = data.data
                $rootScope.showMasker = true;
                $scope.showCerPicker = true;
            })
        };
        $scope.deletedItem = function(index) {
            $scope.param.dealDetails.splice(index, 1);
        };
        $scope.addCertificate = function() {
            $scope.picker = $scope.picker || {};
            var check = false;
            check = checkPicker($scope.picker);
            if (!check) {
                return;
            };
            $scope.param.dealDetails.push($scope.picker);
            // 隐藏表单
            hideCertificate();
        };
        var hideCertificate = function() {
            $scope.picker = {};
            $rootScope.showMasker = false;
            $scope.showCerPicker = false;
        };
        $scope.cacelCertificate = function() {
            hideCertificate();
        };
        // 验证是否输入完整
        var checkPicker = function(data) {
            var check = true;
            if (!data.cerType) {
                Util.Toast("请选择办证类型");
                check = false;
                return check;
            }
            if (!data.personnels) {
                Util.Toast("请填写人员姓名");
                check = false;
                return check;
            }
            return check;

        };
        var getreimParams = function() {
                var reimDetails

                var dealDetails = $scope.param.dealDetails.map(function(item, index) {
                    return {
                        "cerType": item.cerType,
                        "remark": item.remark,
                        "personnels": item.personnels
                    }
                });
                reimDetails = JSON.stringify(dealDetails);
                return reimDetails;
            }
            // 提交申请
        $scope.submit = function() {
            if ($scope.param.dealDetails.length < 1) {
                Util.Toast("请添加办证明细");
                return;
            }
            $scope.param.dealList = getreimParams();
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.certificateDeal;
            var options = {};
            options.urlParam = urlParam;
            var myDest = {};
            options.param = angular.copy($scope.param, myDest);
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            delete options.param.dealDetails
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.certificateDeal
            }
            new FormSubmit(options);
        };
        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
                // 获取选择页面返回的数据并且合并到参数中
                // 用来显示选择的报销明细
                $scope.param.dealDetails = $scope.param.dealDetails || [];

            });
            // 是否显示审批记录
            Util.showloglist($routeParams.id, function(data) {
                $scope.logData = data;
            });
        };
        // 初始化
        init();

    });
